package joodle.domain;

import java.awt.geom.Line2D;
import java.awt.geom.Path2D;

/**
 * The other basic shape included in joodle out of box. It is a subclass of AbstractShape 
 * and inherit most of its methods. The draw, hasEnoughPoints, isPointOn and asXML methods 
 * will be overridden in these basic shapes classes to give the proper functionality to 
 * each of the basic shapes.
 */
public class Line extends AbstractShape {

	public Line() {
		super();
		attributes.remove(Attribute.FILL_COLOR);
	}
	
	protected Line(Line l) {
		super(l);
	}
	
	/**
	 * Creates and updates the line
	 * @see Shape#remakeAwtShape()
	 */
	@Override
	protected void remakeAwtShape() {
		Point p1 = points.get(0);
		Point p2 = points.get(1);

		path = new Path2D.Float(new Line2D.Float(
				p1.getX(), p1.getY(), p2.getX(), p2.getY()));
	}

	/**
	 * Checks if there are enough points to create a Line object
	 * @see Shape#hasEnoughPoints()
	 * @return a boolean value depends on the enough points condition
	 */
	@Override
	public boolean hasEnoughPoints() {
		return points.size() > 1;
	}

	/**
	 * Updates the end point of the line
	 * @see Shape#updateEndPoint()
	 */
	@Override
	public void updateEndPoint(Point p) {
		if(!hasEnoughPoints())
			return;
		points.set(1, p);
		remakeAwtShape();
	}
	
	/**
	 * Creates a clone of the Line shape
	 * @see Shape#cloneShape()
	 * @return a new Line shape
	 */
	@Override
	public Shape cloneShape() {
		return new Line(this);
	}

	/**
	 * Creates a character sequence which is the XML representation of the line
	 * @see Shape#asXML()
	 * @return string includes XML representation of the line
	 */
	@Override
	public String asXML() {
		StringBuilder sb = new StringBuilder();

		Point p1 = points.get(0);
		Point p2 = points.get(1);
		
		sb.append(String.format("<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"",
				p1.getX(), p1.getY(), p2.getX(), p2.getY()));
		
		sb.append(attributesAsXML());
		sb.append(transformAsXML());
		
		sb.append(" />\n");

		return sb.toString();
	}
}
